var gulp = require("gulp"),
    concat = require('gulp-concat'),            /*文件合并*/
    sass = require("gulp-sass"),                /*sass转css*/
    autoprefixer = require('gulp-autoprefixer'),/*浏览器兼容性自动补齐前缀*/
    cssmin = require('gulp-clean-css'),         /*css压缩*/
    cssver = require('gulp-make-css-url-version'),/*添加版本号与gulp-clean-css 配合使用*/
    rename = require('gulp-rename'),            /*重新命名文件或者文件夹*/
    del = require('del'),                       /*删除文件或者文件夹*/
    gulpSequence = require('gulp-sequence'),     /*安顺序执行任务*/
    tiny = require('gulp-tinypng-nokey'),       /*图片压缩*/
    uglify = require('gulp-uglify'),            /*js压缩*/
    babel = require('gulp-babel'),              /*es6转es5*/
    htmlmin = require('gulp-htmlmin')           /*html压缩*/


var baseOutPath='./dest';                   /*输出的跟目录*/
var sassFiles = "./app/css/**/*.scss";     /*sass源文件目录*/
var cssFiles = "./app/css/**/*.css";       /*css源文件目录*/
var imagesFiles='./app/public/images/**';  /*图片资源目录*/
var es6Files='./app/js/**/*.js';            /*es6源文件目录*/
var htmlFiles='./app/views/**/*.html';      /*html源文件目录*/

var sassOutPath=baseOutPath+'/css';              /*sass转换后输出的目录*/
var cssOutPath=baseOutPath+'/css';               /*css压缩后输出的目录*/
var imagesOutPath=baseOutPath+'/public/images';  /*图片压缩输出目录*/
var htmlOutPath=baseOutPath+'/views';             /*html输出目录*/




/*sass转css并压缩*/
gulp.task("sass", () => {
    return  gulp.src(sassFiles)
        .pipe(sass())
        .pipe(autoprefixer("last 3 version","safari 5", "ie 8", "ie 9"))
        .pipe(cssmin())
        .pipe(cssver())
        .pipe(rename({suffix:'.min'}))
        .pipe(gulp.dest(sassOutPath))
});
/*css压缩*/
gulp.task('css',()=>{
    return gulp.src(cssFiles)
        .pipe(cssmin())
        .pipe(rename({suffix:'.min'}))
        .pipe(gulp.dest(cssOutPath))
});
/*图片压缩*/
gulp.task('tinyPng', function () {
    return gulp.src([imagesFiles+'/*.{jpg,png,jpeg}'])
        .pipe(tiny())
        .pipe(gulp.dest(imagesOutPath))
});
/*es6=>es 并压缩*/
gulp.task("babel", function () {
    return gulp.src(es6Files)
        .pipe(babel({"presets": ["env"]}))
        .pipe(uglify())
        .pipe(gulp.dest("./dest/js"));
});

/*html 的压缩*/
gulp.task('html',()=>{
    var options = {
        collapseWhitespace:true,
        collapseBooleanAttributes:true,
        removeComments:true,
        removeEmptyAttributes:true,
        minifyJS:true,
        minifyCSS:true
    };
    /*3.removeComments:清除html中注释的部分，我们应该减少html页面中的注释
    * 4removeEmptyAttributes:清除所有的空属性，
    * */
    return gulp.src(htmlFiles)
        .pipe(htmlmin(options))
        .pipe(rename({suffix:'.min'}))
        .pipe(gulp.dest(htmlOutPath))
});

/*监听文件变动*/
gulp.task("watch", () =>　{
    gulp.watch(sassFiles,["sass"]);
    gulp.watch(cssFiles,["css"]);
    gulp.watch(imagesFiles,["tinyPng"]);
    gulp.watch(es6Files,["babel"]);
    gulp.watch(htmlFiles,["html"]);
});
/*清除目录*/
gulp.task('clean', () => {
    del(['./dest/*']);
});

/*主入口,*/
gulp.task("default",gulpSequence(['clean','html','sass','css','tinyPng','babel']));

// gulp.task("default",gulpSequence(['clean']));
